from ILI9431 import ILI9341
from machine import Pin, I2C, SPI
import time
import net
import urequests
import ujson
spi = SPI(

    2,
    baudrate=40000000,
    miso=Pin(19),
    mosi=Pin(23),
    sck=Pin(18))

display = ILI9341(
    spi,
    cs=Pin(4),
    dc=Pin(15),
    rst=Pin(5),

    w=320,
    h=240,
    r=0)
nic = net.start('STA','MEIZU 16th','15929665250')
a = urequests.get('https://way.jd.com/he/freeweather?city=weinan&appkey=46857c715784593a69c7d24e530127b1')
js = ujson.loads(a.text)
result = js["result"]["HeWeather5"][0]
b = 22
display.erase()
display.vline(120,195,170)
display.hline(0,80,240)
display.hline(0,195,240)
display.printf('城市',60,0)
display.printf('渭南市临渭区',30,20)
display.printf('更新时间',49,42)
display.bitmap(160,0,79,80,'/sd/weather/qzdy.bin')
display.printf('今日天气',85,83)
display.printf('近一小时天气',13,80+b+b+b+b+b+12)
display.printf('建议',40+120,80+b+b+b+b+b+12)

time = result["basic"]["update"]["loc"]

display.printf(time[5:7]+'月'+time[8:10]+'日'+time[11:13]+'时',10,60)

day = result["daily_forecast"][0]["cond"]["txt_d"]
night = result["daily_forecast"][0]["cond"]["txt_n"]
pop = result["daily_forecast"][0]["pop"]
hum = result["daily_forecast"][0]["hum"]
max = result["daily_forecast"][0]["tmp"]["max"]
min = result["daily_forecast"][0]["tmp"]["min"]
sc = result["daily_forecast"][0]["wind"]["sc"]
spd = result["daily_forecast"][0]["wind"]["spd"]
display.printf('白天:%s'%day,0,81+b)
display.printf('夜晚:%s'%night,120,81+b)
display.printf('降水率:%s'%pop,0,81+b+b)
display.printf('湿度:%s'%hum,120,81+b+b)
display.printf('最高温:%s℃'%max,0,81+b+b+b)
display.printf('最低温:%s℃'%min,120,81+b+b+b)
display.printf('风类:%s'%sc,0,81+b+b+b+b)
display.printf('风速:%sm/s'%spd,120,81+b+b+b+b)

w = result["hourly_forecast"][0]["cond"]["txt"]
hum = result["hourly_forecast"][0]["hum"]
tmp = result["hourly_forecast"][0]["tmp"]
spd = result["hourly_forecast"][0]["wind"]["spd"]

display.printf('天气:%s'%w,0,80+b+b+b+b+b+12+b)
display.printf('温度:%s℃'%tmp,0,80+b+b+b+b+b+12+b+b)
display.printf('湿度:%s'%hum,0,80+b+b+b+b+b+12+b+b+b)
display.printf('风速:%sm/s'%spd,0,80+b+b+b+b+b+12+b+b+b+b)

suggestion = result["suggestion"]["comf"]["txt"]
display.printf(suggestion,3+120,77+b+b+b+b+b+12+b)
